#include "export.h"
#include "ui_export.h"
#include<QDesktopWidget>
#include<QtSql/QSqlQuery>
#include<QMessageBox>
#include<QDebug>
#include<QTextStream>
#include<QFile>
//This class export all expense records to thr .csv file
Export::Export(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Export)
{
    ui->setupUi(this);
    QRect q=QApplication::desktop()->availableGeometry(this);
    ui->scrollArea->setGeometry(0,0,(q.width()),(q.height()));
    ui->progressBar->setFocus();
    int fontSize=10;
    QAction* backAction = new QAction( tr("Back"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );

    ui->label->setFont(QFont("lucida",(fontSize-1),QFont::Bold,true));
    QMessageBox msg;
    msg.setInformativeText("This will export expense records to C:\\Data\\backup.csv Do you want to continue?");
    msg.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
    msg.setDefaultButton(QMessageBox::Yes);
    int res=msg.exec();
    if(res==QMessageBox::No)
    {
        ui->progressBar->setVisible(false);
         ui->label->setText(tr("No record is exported"));
    }
    else if(res==QMessageBox::Yes)
    {

    db=QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("finance_db");
    if(!db.open())
    {
        QMessageBox::information(0,"Error !","Error in opning database");
        (new MainWindow)->show();
    }
    else
    {
        QFile f("C:\\backup.csv");
        /*if(f.exists())
            f.open(QIODevice::Append);
        else*/
            f.open(QIODevice::WriteOnly);
        QSqlQuery q;
        if(q.exec(QString("select count(*) from expense_table")))
        {
            q.next();
            double rowCount=q.value(0).toDouble();
           if(rowCount>0)
            {
               int step=(ui->progressBar->maximum()/rowCount);
               int value=0,accid=0;
               QString aname;
               QSqlQuery qry;
               if(q.exec(QString("select cat_name,exp_amt,exp_date,note,acc_id from expense_table")))
               {
                   ui->label->setText(tr("Backup is processing..."));
                   while(q.next())
                   {
                       value=value+step;
                       accid=q.value(4).toInt();
                       qry.exec(QString("select name from account_table where account_id=%1").arg(accid));
                       if(qry.next())
                           aname=qry.value(0).toString();
                       else
                           aname="Unknown";
                       QTextStream t(&f);

                       t<<aname<<",";
                       t<<q.value(0).toString()<<",";
                       t<<q.value(1).toDouble()<<",";
                       t<<q.value(2).toDate().toString()<<",";
                       t<<q.value(3).toString()<<",";
                       t<<"\n";
                       ui->progressBar->setValue(value);
                   }
                   ui->progressBar->setValue(100);
                   ui->label->setText("Backup process is completed.Location of backup file C:\\Data\\backup.csv");
                  // ui->label_2->setText(tr(""));
               }
           }
           else
           {
               ui->progressBar->setVisible(false);
               ui->label->setText(tr("We are sorry! No expense record is present to export"));

    }//Else expense record is present
    }
        else
        {
            ui->progressBar->setVisible(false);
            ui->label->setText(tr("We are sorry! No expense record is present to export"));
        }
        f.close();


    }
}
}

Export::~Export()
{
    delete ui;
}
void Export::back()
{
    (new MainWindow)->show();
}
